<template>
  <div class="app-container">
    <el-table
      :data="onlineUserList"
      style="width: 100%;margin-bottom: 20px;margin-top: 10px;"
      row-key="id"
      border
      :default-expand-all="false"
      :tree-props="{children: 'children'}"
    >

      <el-table-column label="登陆名称" align="center" prop="userName" :show-overflow-tooltip="true"/>
      <el-table-column label="部门名称" align="center" prop="deptName" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" class-name="small-padding fixed-width" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" icon="Delete" @click="handleForceLogout(scope.row)">强退</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { getOnlineInfo, forceLogout } from '@/api/monitor/server'

export default {
  // 定义数据
  data() {
    return {
      onlineUserList: []
    }
  },

  // 当页面加载时获取数据
  created() {
    this.fetchData()
  },

  methods: {
    fetchData() {
      getOnlineInfo().then(response => {
        this.onlineUserList = response.data
      })
    },

    handleForceLogout(row) {
      this.$confirm('是否确认强退名称为' + row.userName + '的用户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用方法强退
        forceLogout(row.userName)
          .then(res => {
            if (res.data) {
              this.$message({
                type: 'success',
                message: '强退成功!'
              })
              // 刷新
              this.fetchData()
            }
          })
      })
    }

  }
}
</script>
